package edu.hawaii.myisern.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import edu.hawaii.myisern.collaborations.jaxb.Collaboration;
import edu.hawaii.myisern.model.MyIsernModel;
import edu.hawaii.myisern.organizations.jaxb.Organization;
import edu.hawaii.myisern.researchers.jaxb.Researcher;

/**
 * Handles the Printing of the Researcher, Organization
 * and Collaboration objects.
 * 
 * @author Sonwright M. Gomez
 *
 */
public class DisplayActionBean extends MyIsernActionBean {
  
  /** The singleton MyIsernModel. */
  private MyIsernModel myIsern = MyIsernModel.getInstance();
  /** An error string, always displayed, but invisible if empty. */
  private String errorMessage = "";
  List<String> researchers = new ArrayList<String>();
  List<String> organizations = new ArrayList<String>();
  List<String> collaborations = new ArrayList<String>();
  
  /**
   * Returns the error message, which may be empty.
   * 
   * @return The error message. 
   */
  public String getErrorMessage() {
    return this.errorMessage;
  }
  
  /**
   * Set's the error message for the user.
   * 
   * @param errorMessage The error message. 
   */
  public void setErrorMessage(String errorMessage) {
    this.errorMessage = errorMessage;
  }
  
  /**
   * Provides an iterator over the Collaboration list to the page. 
   * @return The Collaboration iterator.
   */
  public Iterator<Collaboration> getCollaborationIterator() {
    return myIsern.cIterator();
  }
  
  /**
   * Provides an iterator over the Researcher list to the page. 
   * @return The Researcher iterator.
   */
  public Iterator<Researcher> getResearcherIterator() {
    return myIsern.rIterator();
  }
  
  /**
   * Provides an iterator over the Researcher list to the page. 
   * @return The Researcher iterator.
   */
  public Iterator<Organization> getOrganizationIterator() {
    return myIsern.oIterator();
  }
    
  /**
   * Prints Researchers
   * @return ForwardResolution to redisplay the display.jsp page
   */
  public Resolution printResearchers() {
    
    return new ForwardResolution("/displayResearchers.jsp");
  }
  
  /**
   * Prints Organizations
   * @return ForwardResolution to redisplay the display.jsp page
   */
  public Resolution printOrganizations () {
    return new ForwardResolution("/displayOrganizations.jsp");
  }
  
  /**
   * Prints Collaborations
   * @return ForwardResolution to redisplay the display.jsp page
   */
  public Resolution printCollaborations () {
    return new ForwardResolution("/displayCollaborations.jsp");
  }
  
  /**
   * Display the searching bar for editing a researcher.
   * @return ForwardResolution to display the editResearcherPage.jsp page.
   */
  public Resolution editResearcher () {
    
    List<String> tempRList = new ArrayList<String>();
    researchers.addAll(myIsern.getResearcherIds());
    for (String tempRes : researchers) {
      tempRList.add(tempRes.replace('_', ' '));
    }
    Collections.sort(tempRList);
    getContext().getRequest().getSession().setAttribute("researchersList", tempRList);
    return new ForwardResolution("/findResearcher.jsp");
  }
  
  /**
   * Display the searching bar for editing a organization.
   * @return ForwardResolution to display the editOrganizationPage.jsp page.
   */
  public Resolution editOrganization () {
    
    List<String> tempOrgList = new ArrayList<String>();
    organizations.addAll(myIsern.getOrganizationIds());
    for (String tempOrg : organizations) {
      tempOrgList.add(tempOrg.replace('_', ' '));
    }
    Collections.sort(tempOrgList);
    getContext().getRequest().getSession().setAttribute("organizationsList", tempOrgList);
  
    return new ForwardResolution("/findOrganization.jsp");
  }
  
  /**
   * Display the searching bar for editing a collaboration.
   * @return ForwardResolution to display the editCollaborationPage.jsp page.
   */
  public Resolution editCollaboration () {
    
    List<String> tempCollabList = new ArrayList<String>();
    collaborations.addAll(myIsern.getCollaborationIds());
    for (String tempCollab : collaborations) {
      tempCollabList.add(tempCollab.replace('_', ' '));
    }
    Collections.sort(tempCollabList);
    getContext().getRequest().getSession().setAttribute("collaborationsList", tempCollabList);
    
    return new ForwardResolution("/findCollaboration.jsp");
  }
  
  /**
   * Display the add researcher jsp page.
   * @return ForwardResolution to display the add Researcher page.
   */
  public Resolution addResearcher () {
    
    List<String> tempOrgList = new ArrayList<String>();
    organizations.addAll(myIsern.getOrganizationIds());
    for (String org : organizations) {
      tempOrgList.add(org.replace('_', ' '));
    }
    Collections.sort(tempOrgList);
    getContext().getRequest().getSession().setAttribute("organizationsList", tempOrgList);
    
    return new ForwardResolution("/addResearcher.jsp");
  }
}
